package com.zdj.websocket.config;

import com.zdj.websocket.interceptor.HttpHandShakeInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;

/**
 * @author zhangdj
 */
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

    /**
     * 注册端点，发布或者订阅消息的时候需要链接此断电
     * setAllowedOrigins *表示允许所有域建立链接
     * withSockJS 开启socketJS支持
     * @param registry
     */
    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("endpoint-websocket")
                .addInterceptors(new HttpHandShakeInterceptor())
                .setAllowedOrigins("*")
                .withSockJS();
    }


    /**
     * 配置消息代理
     * setApplicationDestinationPrefixes 客户端发送数据给服务端的一个前缀
     * enableSimpleBroker 服务端推送给客户端的路径前缀
     * @param registry
     */
    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
//        registry.enableSimpleBroker("/topic");
        registry.setApplicationDestinationPrefixes("/app");
    }
}
